<?php

declare(strict_types=1);
/**
 * This file is part of bearing-web.
 *
 * @link     https://www.bearing.cn/
 * @contact  tech@bearing.cn
 * @license  https://gitee.com/cppo/bearing_web/-/blob/master/README.md
 */

namespace common\models;

use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\db\BaseActiveRecord;

/**
 * This is the model class for table "{{%product_detail}}".
 *
 * @property int $id
 * @property int $product_id
 * @property int $cate_id
 * @property null|string $attr
 * @property int $create_time
 * @property int $update_time
 */
class ProductDetailModel extends ActiveRecord
{
    public static function tableName(): string
    {
        return '{{%product_detail}}';
    }

    public function rules(): array
    {
        return [
            [['attr'], 'default', 'value' => null],
            [['update_time'], 'default', 'value' => 0],
            [['product_id', 'cate_id', 'create_time', 'update_time'], 'integer'],
            [['attr'], 'string'],
        ];
    }

    public function attributeLabels(): array
    {
        return [
            'id' => 'ID',
            'product_id' => 'Product ID',
            'cate_id' => 'Cate ID',
            'attr' => 'Attr',
            'create_time' => 'Create Time',
            'update_time' => 'Update Time',
        ];
    }

    /**
     * 时间更新策略.
     *
     * @return array[]
     */
    public function behaviors(): array
    {
        return [
            [
                'class' => TimestampBehavior::class,
                'attributes' => [
                    BaseActiveRecord::EVENT_BEFORE_INSERT => ['create_time', 'update_time'],   // 创建
                    BaseActiveRecord::EVENT_BEFORE_UPDATE => ['update_time'],       // 修改
                ],
                'value' => function () {
                    return time();
                },
            ],
        ];
    }
}
